<template>
  <div class="app-container">
    <div style="width: 50%">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="头衔">
          <el-select v-model="form.level" placeholder="讲师头衔">
            <el-option label="高级讲师" value="1" />
            <el-option label="首席讲师" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            :min="1"
            :max="10"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="头像">
          <pan-thumb :image="form.avatar" />
          <el-button
            type="primary"
            icon="el-icon-ipload"
            @click="imagecropperShow = true"
            >更换头像</el-button
          >
          <image-cropper
          v-show="imagecropperShow"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="BASE_API + '/eduoss/fileoss/upload'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"
          />
        </el-form-item>
        
        <el-form-item label="简介">
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            placeholder="讲师简介"
            v-model="form.intro"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="资历">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 6 }"
            placeholder="讲师资历"
            v-model="form.career"
          >
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
  
    </div>
  </div>
</template>
  
  <script>
import teacher from "@/api/teacher";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";
export default {
    components:{ImageCropper,PanThumb},
  data() {
    return {
      form: {
        id: "",
        name: "",
        intro: "",
        level: "",
        career: "",
        avatar: "",
        sort: 0,
        isDeleted: "",
      },
      imagecropperShow:false,
      imagecropperKey:0,
      BASE_API: process.env.BASE_API,
    };
  },
  created() {
    this.init();
  },
  watch: {
    $route(to, from) {
      this.init();
    },
  },
  methods: {
    close(){
        this.imagecropperShow = false
    },
    cropSuccess(data){
        this.imagecropperShow = false
        this.form.avatar = data.url
        this.imagecropperKey = this.imagecropperKey+1
    },
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id;
        this.getTeacher(id);
      } else {
        this.form = {};
      }
    },
    onSubmit() {
      if (this.form.id) {
        this.updateTeaherInfo();
      } else {
        this.saveTeacher();
      }
    },
    getTeacher(id) {
      teacher.getTeacherInfo(id).then((response) => {
        this.form = response.data.item;
        this.form.level = this.form.level == 1 ? "高级讲师" : "首席讲师";
      });
    },
    saveTeacher() {
      teacher.saveTeaher(this.form).then((response) => {
        if (response.state == "20000") {
          this.$message({
            type: "success",
            message: "讲师添加成功",
          });
          this.$router.push({ path: "/teacher/table" });
        }
      });
      this.form = {};
    },
    updateTeaherInfo() {
      this.form.level = this.form.level == "高级讲师" ? 1 : 2;
      teacher.updateTeaher(this.form).then((response) => {
        if (response.state == "20000") {
          this.$message({
            type: "success",
            message: "修改添加成功",
          });
          this.$router.push({ path: "/teacher/table" });
        }
      });
      this.form = {};
    },
    onCancel() {
      this.$message({
        message: "操作取消!",
        type: "warning",
      });
      this.form = {};
    },
  },
};
</script>
  
  <style scoped>
.line {
  text-align: center;
}
</style>
  
  